/** ============================================================================================== **/
/**						PLANT MANAGER											 				   **/	
/** ============================================================================================== **/

BootApplication.controller("ViewProfileController",
			   [ "$scope",	"$rootScope", '$timeout', '$translate', "NgTableParams", "ViewProfileService", '$http', 'configuration',
		function ($scope, 	 $rootScope,   $timeout,   $translate,   NgTableParams,   viewProfileService,   $http,   configuration) {
	
	console.log("ViewProfileController");
	
	$scope.userModel			= {};
	$scope.years 				= [{"label":"1999", "selected":"false"},{"label":"1998", "selected":"false"},{"label":"1997", "selected":"false"},{"label":"1996", "selected":"false"},{"label":"1995", "selected":"false"},{"label":"1994", "selected":"false"},{"label":"1993", "selected":"false"},{"label":"1992", "selected":"false"},{"label":"1991", "selected":"false"},{"label":"1990", "selected":"false"},{"label":"1989", "selected":"false"},{"label":"1988", "selected":"false"},{"label":"1987", "selected":"false"},{"label":"1986", "selected":"false"},{"label":"1985", "selected":"false"},{"label":"1984", "selected":"false"},{"label":"1983", "selected":"false"},{"label":"1982", "selected":"false"},{"label":"1981", "selected":"false"},{"label":"1980", "selected":"false"},{"label":"1979", "selected":"false"},{"label":"1978", "selected":"false"},{"label":"1977", "selected":"false"},{"label":"1976", "selected":"false"},{"label":"1975", "selected":"false"},{"label":"1974", "selected":"false"},{"label":"1973", "selected":"false"},{"label":"1972", "selected":"false"},{"label":"1971", "selected":"false"},{"label":"1970", "selected":"false"},{"label":"1969", "selected":"false"},{"label":"1968", "selected":"false"},{"label":"1967", "selected":"false"},{"label":"1966", "selected":"false"},{"label":"1965", "selected":"false"},{"label":"1964", "selected":"false"},{"label":"1963", "selected":"false"},{"label":"1962", "selected":"false"},{"label":"1961", "selected":"false"},{"label":"1960", "selected":"false"},{"label":"1959", "selected":"false"},{"label":"1958", "selected":"false"},{"label":"1957", "selected":"false"},{"label":"1956", "selected":"false"},{"label":"1955", "selected":"false"},{"label":"1954", "selected":"false"},{"label":"1953", "selected":"false"},{"label":"1952", "selected":"false"},{"label":"1951", "selected":"false"},{"label":"1950", "selected":"false"},{"label":"1949", "selected":"false"},{"label":"1948", "selected":"false"},{"label":"1947", "selected":"false"},{"label":"1946", "selected":"false"},{"label":"1945", "selected":"false"},{"label":"1944", "selected":"false"},{"label":"1943", "selected":"false"},{"label":"1942", "selected":"false"},{"label":"1941", "selected":"false"},{"label":"1940", "selected":"false"},{"label":"1939", "selected":"false"},{"label":"1938", "selected":"false"},{"label":"1937", "selected":"false"},{"label":"1936", "selected":"false"},{"label":"1935", "selected":"false"},{"label":"1934", "selected":"false"},{"label":"1933", "selected":"false"},{"label":"1932", "selected":"false"},{"label":"1931", "selected":"false"},{"label":"1930", "selected":"false"},{"label":"1929", "selected":"false"},{"label":"1928", "selected":"false"},{"label":"1927", "selected":"false"},{"label":"1926", "selected":"false"},{"label":"1925", "selected":"false"},{"label":"1924", "selected":"false"},{"label":"1923", "selected":"false"},{"label":"1922", "selected":"false"},{"label":"1921", "selected":"false"},{"label":"1920", "selected":"false"},{"label":"1919", "selected":"false"},{"label":"1918", "selected":"false"},{"label":"1917"}];
	$scope.days 				= [{"label":"1", "selected":"false"},{"label":"2", "selected":"false"},{"label":"3", "selected":"false"},{"label":"4", "selected":"false"},{"label":"5", "selected":"false"},{"label":"6", "selected":"false"},{"label":"7", "selected":"false"},{"label":"8", "selected":"false"},{"label":"9", "selected":"false"},{"label":"10", "selected":"false"},{"label":"11", "selected":"false"},{"label":"12", "selected":"false"},{"label":"12", "selected":"false"},{"label":"13", "selected":"false"},{"label":"14", "selected":"false"},{"label":"15", "selected":"false"},{"label":"16", "selected":"false"},{"label":"17", "selected":"false"},{"label":"18", "selected":"false"},{"label":"19", "selected":"false"},{"label":"20", "selected":"false"},{"label":"21", "selected":"false"},{"label":"22", "selected":"false"},{"label":"23", "selected":"false"},{"label":"24", "selected":"false"},{"label":"25", "selected":"false"},{"label":"26", "selected":"false"},{"label":"27", "selected":"false"},{"label":"28", "selected":"false"},{"label":"29", "selected":"false"},{"label":"30", "selected":"false"},{"label":"31"}];
	$scope.months 				= [{"label":"Gennaio", "value":"1", "selected":"false"},{"label":"Febbraio", "value":"2", "selected":"false"},{"label":"Marzo", "value":"3", "selected":"false"},{"label":"Aprile", "value":"4", "selected":"false"},{"label":"Maggio", "value":"5", "selected":"false"},{"label":"Giugno", "value":"6", "selected":"false"},{"label":"Luglio", "value":"7", "selected":"false"},{"label":"Agosto", "value":"8", "selected":"false"},{"label":"Settembre", "value":"9", "selected":"false"},{"label":"Ottobre", "value":"10", "selected":"false"},{"label":"Novembre", "value":"11", "selected":"false"},{"label":"Dicembre", "value":"12"}];
	$scope.showSuccessSave 		= false;
	
	$scope.getUserProfile = function () {
		console.log("view-profile.js		>>>	Recupero il profilo dell'utente");
		$scope.idUser		= $rootScope.idUser;
		viewProfileService.getUserProfile( ).then(function( response ){
			if ( response!=null && response.data!=undefined && response.data.aaData!=null ){
				$scope.userModel 	= response.data.aaData;
				$.each( $scope.years, function( index, year ){
					if (year.label==$scope.userModel.dateBirth.year) year.selected="true";
				});
				$.each( $scope.months, function( index, month ){
					if (month.value==$scope.userModel.dateBirth.monthValue) month.selected="true";
				});
				$.each( $scope.days, function( index, day ){
					if (day.label==$scope.userModel.dateBirth.dayOfMonth) day.selected="true";
				});
			}
		});
	}
	$scope.$watch('idUser', function() {
		if ($rootScope.idUser!=undefined) {
			$scope.getUserProfile();
		}
    });
	
	/*******************************************************************************************************	USER VIEW 	   ***********	START	*/
    $scope.userModel		= {};
    $scope.errorMessage		= "";
    $scope.saveUser = function ( event ) {
    	console.log("view-profile-ctrl.js		>>>	Salvo le modifiche al profile");
    	
    	if ( $scope.validatorUser() ) {
    	
    		viewProfileService.saveUser( $scope.userModel ).then(function( response ){
				if (response!=null && response.data!=null && response.data.success) {
					console.log("view-profile-ctrl.js		>>>	Le modifiche sono state salvate con successo");
					$scope.showSuccessSave = true;
					$timeout(function () {
						$scope.showSuccessSave = !$scope.showSuccessSave;
					}, 2000)
				} else
		    		$scope.showAlert('Errore',$scope.errorMessage);
			});
    	} else
    		$scope.showAlert('Errore',$scope.errorMessage);
    }
    $scope.validatorUser = function ( ) {
    	var resultValidation = true;
    	/** BASE VALIDATOR **/
    	if ($scope.userModel.firstname==undefined || $scope.userModel.firstname=="") {
    		$scope.errorMessage="Non e' stato definito il firstname";
    		resultValidation=false;
    	} else if ($scope.userModel.lastname==undefined || $scope.userModel.lastname=="") {
    		$scope.errorMessage="Non e' stato definito il lastname";
    		resultValidation=false;
    	} else if ($scope.userModel.email==undefined || $scope.userModel.email=="") {
    		$scope.errorMessage="Non e' stata definita la email";
    		resultValidation=false;
    	}
    	return resultValidation;
    }
    /*******************************************************************************************************	USER VIEW 	   		***********	END	*/
}]);